package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"math"
	"qytest/models"
	"strconv"
	"strings"
	"time"
)

func (c *MainController)ShowIndex() {

	userName := c.GetString("userName")
	//name := []byte(userName)
	var queryName []string
	qs := o.QueryTable("UserManage")
	var users []models.UserManage
	var userss []models.UserManage
	var query string

	count, err := qs.Count()
	if err != nil {
		beego.Info("查询错误")
		return
	}
	qs.All(&users)
	userss = users

	for _,v := range users{
		if strings.Contains(v.UserName,userName) == true{
			queryName = append(queryName,v.UserName)
		}
	}
	if len(queryName) > 0 {

		count,err = qs.Filter("UserName__in",queryName).Count()
		if err != nil {
			beego.Info("查询错误")
			return
		}
	}else{
		userss = []models.UserManage{}
		count = 0
	}


	pageSize := 5   //每页条数
	pageIndex,err := strconv.Atoi(c.GetString("pageIndex"))
	if err != nil{
		pageIndex = 1
	}
	start := pageSize*(pageIndex-1)

	pageCount := math.Ceil(float64(count)/float64(pageSize))
	pageUp := pageIndex-1
	pageDown := pageIndex+1
	if pageUp < 1 {
		pageUp = 1
	}
	if pageDown > int(pageCount) {
		pageDown = int(pageCount)
	}

	if len(queryName) > 0 {
		qs.Limit(pageSize, start).Filter("UserName__in",queryName).All(&userss)
	}

	query = userName

	test := "用户管理"
	//for _,v := range users{
	//	if v.UserName == userName{
	//		fmt.Println(1111)
	//	}
	//}


	c.Data["count"] = count
	c.Data["pageCount"] = pageCount
	c.Data["pageIndex"] = pageIndex
	c.Data["pageUp"] = pageUp
	c.Data["pageDown"] = pageDown
	c.Data["users"] = userss
	c.Data["query"] = query
	c.Data["test"] = test


	c.Layout = "layout.html"
	c.TplName = "container.html"
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["Scripts"] = "container_script.html"
}



func (c*MainController)ShowAdd()  {

	test := "添加用户"
	c.Data["test"] = test


	c.Layout = "layout.html"
	c.TplName = "addUser.html"
}
//
func (c*MainController)HandleAdd()  {
	userName := c.GetString("userName")
	userId := c.GetString("userId")
	validTime := c.GetString("validTime")
	longForm := "2006-01-02 15:04:05"
	t,err := time.ParseInLocation(longForm,validTime, time.Local)
	if err != nil{
		beego.Info("时间格式错误")
	}
	user := models.UserManage{}
	user.UserId = userId
	user.UserName = userName
	user.RegistTime = time.Now()
	user.DueTime = t
	_,err = o.Insert(&user)
	if err != nil {
		beego.Info("插入错误")
		return
	}

	c.Redirect("/Manage/user/index",302)
}

func (c*MainController)HandleDelete()  {
	UserId := c.GetString("UserId")
	user := models.UserManage{UserId: UserId}
	err := o.Read(&user)
	if err != nil {
		beego.Info("查询数据错误")
		return
	}
	o.Delete(&user)

	c.Redirect("/Manage/user/index",302)

}


func (c*MainController)ShowUpdate()  {
	UserId := c.GetString("UserId")
	c.Data["UserId"] = UserId

	test := "编辑用户"
	c.Data["test"] = test

	c.Layout = "layout.html"
	c.TplName = "updateUser.html"
	}


func (c*MainController)HandleUpdate()  {
	UserId := c.GetString("UserId")
	fmt.Println(UserId)
	user := models.UserManage{UserId: UserId}
	err := o.Read(&user)
	if err != nil {
		beego.Info("查询数据错误")
		return
	}
	validTime := c.GetString("validTime")
	longForm := "2006-01-02 15:04:05"
	t,err := time.ParseInLocation(longForm,validTime, time.Local)
	if err != nil{
		beego.Info("时间格式错误")
	}
	user.DueTime = t
	_, err = o.Update(&user)
	if err != nil {
		beego.Info("更新资源错误")
		return
	}
	c.Redirect("/Manage/user/index", 302)
}
